<?php
class Level extends AdminController {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        $this->_data["loadPage"]="level/index_view";
        $this->_data["titlePage"]="Quản lý level";
        $this->_data["mess_succ"]=$this->session->flashdata("flash_mess");
        
        $this->load->model("mlevel");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/level/index/",
            "total_rows"=>$this->mlevel->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mlevel->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mlevel->listAllLimit($config["per_page"], $start);
            //$this->_data["info"]=$this->muser->listAll();
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function add() {
        $name=$this->input->post("name");
        $note=$this->input->post("note");
        $order=$this->input->post("order");
        $this->load->model("mlevel");
        if($this->mlevel->checkName($name) == false)
            echo "name";
        else {
            $data=array(
                "level_name"=>$name,
                "level_note"=>$note,
                "level_order"=>$order
            );
            $this->mlevel->insert($data);
            $this->session->set_flashdata("flash_mess", "Thêm level thành công.");
            echo "Thêm level thành công!";
        }
    }
    
    public function edit() {
        $this->load->helper("level_helper");
        $this->_data["loadPage"]="level/edit_view";
        $this->_data["titlePage"]="Chỉnh sửa level";
        
        //load data
        $id=$this->uri->segment(4);
        $this->load->model("mlevel");
        $temp=$this->mlevel->getById($id);
        if($temp!=false)
            $this->_data["info_edit"]=$temp;
        
        //validate
        $this->load->helper("form");
        $this->load->library("form_validation");
        $this->form_validation->CI =& $this;
        
        $config=array(
            array(
                "field"=>"txtName",
                "label"=>"Tên",
                "rules"=>"callback_checkName"
            ),
            array(
                "field"=>"txtOrder",
                "label"=>"Vị trí",
                "rules"=>"required"
            )
        );
        $this->form_validation->set_rules($config);
        
        //update
        if($this->form_validation->run()) {
            $data=array(
                "level_name"=>$this->input->post("txtName"),
                "level_note"=>$this->input->post("txtNote"),
                "level_order"=>$this->input->post("txtOrder")
            );
            $this->mlevel->update($data, $id);
            $this->session->set_flashdata("flash_mess", "Chỉnh sửa level thành công.");
            redirect(base_url()."admin_combo/level/");
        }
        
        $this->load->view($this->_data["path"], $this->_data);
    }
    
    public function del() {
        $id=$this->uri->segment(4);
        $this->load->model("mlevel");
        $this->mlevel->del($id);
        $this->session->set_flashdata("flash_mess", "Xóa level thành công.");
        redirect(base_url()."admin_combo/level/");
    }
    
    public function search() {
        $this->session->set_userdata("sess_search_name", $this->input->post("name"));
        echo "hello";
    }
    
    public function reloadData() {
        $this->load->model("mlevel");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/level/index",
            "total_rows"=>$this->mlevel->countAll(),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mlevel->listAllLimit($config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mlevel->listAllLimit($config["per_page"], $start);
            //$this->_data["info"]=$this->muser->listAll();
        $this->load->view("level/reload_view", $this->_data);
    }
    
    public function reloadDataSearch() {
        $name=$this->session->userdata("sess_search_name");
        $this->session->unset_userdata("sess_search_name");
        $this->load->model("mlevel");
        $this->load->library("pagination");
        $config=array(
            "base_url"=>base_url()."admin_combo/level/index",
            "total_rows"=>$this->mlevel->countAll($name, $this->_data["perpage"], $this->uri->segment(4)),
            "per_page"=>$this->_data["perpage"],
            "uri_segment"=>4
        );
        $start=$this->uri->segment(4)==""?0:$this->uri->segment(4);
        $this->pagination->initialize($config);
        if($this->mlevel->getLikeName($name, $config["per_page"], $start) == false)
            $this->_data["info"] = "empty";
        else
            $this->_data["info"]=$this->mlevel->getLikeName($name, $config["per_page"], $start);
        $this->load->view("level/reload_view", $this->_data);
    }
    
    public function checkName($name) {
        $this->load->model("mlevel");
        $id=$this->uri->segment(4);
        if(!$this->mlevel->checkName($name, $id)) {
            $this->form_validation->set_message("checkName", "Tên này đã được đăng ký!");
            return false;
        }else
            return true;
    }
}
?>